<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="dAdd">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="scope">
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" width="280">
            <template v-slot="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="dDel(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="total,sizes,prev, pager, next"
            :total="total"
            @current-change="CurrentChange"
            @size-change="hSizeChange"
            :page-size="size"
            :current-page.sync="page"
            :page-sizes="[1, 3, 5, 7, 10]"
          />
        </el-row>
      </el-card>
    </div>
    <el-dialog title="新增员工" :visible.sync="dialogVisible">
      <addorEdit @close="hclose" v-if="dialogVisible"/>
    </el-dialog>
  </div>
</template>

<script>
import addorEdit from "./empDialog";
// 枚举
import EmployeeEnum from "@/constant/employees";
import { getEmployeeList, delEmployee } from "@/api/employees";
export default {
  components: { addorEdit },
  data() {
    return {
      dialogVisible:false,
      total: 1, // 一共几条
      page: 1, //第几页
      size: 10, // 每页几条
      list: [],
    };
  },
  created() {
    this.loadEmployeeList();
  },
  methods: {
    CurrentChange(curpage) {
      // 更新页码
      this.page = curpage;
      // 重发请求
      this.loadEmployeeList();
    },
    hSizeChange(curSize) {
      // 更新每页几条
      this.size = curSize;
      this.page = 1;
      // 重发请求
      this.loadEmployeeList();
    },
    async loadEmployeeList() {
      const res = await getEmployeeList(this.page, this.size);
      console.log("获取员工列表", res);
      this.list = res.data.rows;
      this.total = res.data.total;
    },
    formatEmployment(num) {
      const item = EmployeeEnum.hireType.find((item) => item.id === num);
      if (item) {
        return item.value;
      } else {
        return "未知";
      }
    },
    // 删除
    dDel(id) {
      this.$confirm("此操作将删除部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 调用删除api
          await delEmployee(id);
          // 刷新页面
          if (this.list.length === 1) {
            this.page--;
            if (this.page === 0) {
              this.page === 1;
            }
          }
          this.loadEmployeeList();
        })
        .catch(() => {
          // 用户取消退出
        });
    },
    // 新增
    dAdd(){
      this.dialogVisible=true
    },
    hclose(){
      this.dialogVisible=false

    }
  },
};
</script>

